using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Pampa.WebControls;

namespace Pampa.WebControls
{
    public abstract class Button : BaseControl
    {
        protected string text;
        protected ButtonType type;
        protected string iconPath;
        protected string iconPathOn;

        protected bool rollOver;
        protected bool captureEnter;
        protected bool focuseable;

        #region Constructores

        public Button()
        {
            this.CssClass = "buttonDefault";
            this.Text = "Button";
            this.RollOver = true;
        }

        public Button(string ID, ButtonType type)
        {
            this.ID = ID;
            this.type = type;
            this.CssClass = "buttonDefault";
            this.RollOver = true;
        }

        #endregion

        #region Properties

        public string Text
        {
            get { return text; }
            set { this.text = value; }
        }

        public ButtonType Type
        {
            get { return type; }
            set { type = value; }
        }

        public string IconPath
        {
            get { return iconPath; }
            set { iconPath = value; }
        }

        public string IconPathOn
        {
            get { return iconPathOn; }
            set { iconPathOn = value; }
        }

        public bool RollOver
        {
            get { return rollOver; }
            set { rollOver = value; }
        }

        public bool CaptureEnter
        {
            get { return captureEnter; }
            set { captureEnter = value; }
        }

        public bool Focuseable
        {
            get { return focuseable; }
            set { focuseable = value; }
        }

        protected string ObjectName
        {
            get { return "btn" + this.ID.Replace(".", "_"); }
        }
        
        #endregion

        #region Load

        protected override void OnLoad(EventArgs e)
        {
            if (this.CssClass == "buttonDefault")
                RegisterStyleSheet("Pampa.WebControls/Button.css");

            RegisterLibrary("Pampa.Common/Common.js");
            RegisterLibrary("Pampa.WebControls/Button.js");

            base.OnLoad(e);
        }

        #endregion

        #region Render

        protected override void Render(HtmlTextWriter writer)
        {
            if (this.ID != null)
            {
                writer.AddAttribute("id", this.ID);
                writer.AddAttribute("name", this.ID);
            }

            if (!string.IsNullOrEmpty(this.Height.ToString()))
                writer.AddStyleAttribute("height", this.Height.ToString());
            if (!string.IsNullOrEmpty(this.Width.ToString()))
                writer.AddStyleAttribute("width", this.Width.ToString());

            string method = GetMethod();

            switch (type)
            {
                case ButtonType.Button:
                    RenderCommonButton(writer, method);
                    break;

                case ButtonType.Image:
                    RenderImageButton(writer, method);
                    break;

                default:
                    break;
            }

            RenderDefinitions(writer, method);
        }

        private void RenderCommonButton(HtmlTextWriter writer, string method)
        {
            writer.AddAttribute("class", this.CssClass);
            writer.AddAttribute("classRollOver", this.CssClass + "On");

            if (!this.Focuseable)
                writer.AddAttribute("tabindex", "-1");

            if (this.Enabled)
            {
                if (!string.IsNullOrEmpty(method))
                    writer.AddAttribute("onclick", method + "; return false;");
                else
                    writer.AddAttribute("onclick", "return false;");

                if (this.RollOver)
                {
                    writer.AddAttribute("onmouseover", "Common.on(this)");
                    writer.AddAttribute("onmouseout", "Common.off(this)");
                }
            }
            else
            {
                writer.AddStyleAttribute("cursor", "default");
            }

            writer.RenderBeginTag(HtmlTextWriterTag.Button);
            writer.WriteLine(text); 
            writer.RenderEndTag();        
        }

        private void RenderImageButton(HtmlTextWriter writer, string method)
        {
            writer.AddAttribute("src", GetResourceUrl(this.IconPath));
            writer.AddAttribute("alt", this.Text);
            writer.AddAttribute("title", this.Text);
            writer.AddAttribute("class", this.CssClass);

            if (this.Enabled)
            {
                if (!string.IsNullOrEmpty(method))
                    writer.AddAttribute("onclick", method + "return false;");
                else
                    writer.AddAttribute("onclick", "return false;");

                if(this.RollOver)
                {
                    if (String.IsNullOrEmpty(this.IconPathOn))
                        writer.AddAttribute("classRollOver", this.CssClass + "On");
                    else
                        writer.AddAttribute("srcOn", this.IconPathOn);

                    writer.AddAttribute("srcOn", GetResourceUrl(this.iconPathOn));
                    writer.AddAttribute("onmouseover", "Common.on(this)");
                    writer.AddAttribute("onmouseout", "Common.off(this)");
                }
            }
            else
            {
                writer.AddStyleAttribute("cursor", "default");
            }

            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
        }

        private void RenderDefinitions(HtmlTextWriter writer, string method)
        {
            writer.WriteLine("<script type=\"text/javascript\">");
            
            if (this.CaptureEnter)
            {
                writer.Write("Button.registerMethod(\"" + this.ID + "\", \"" + method + "\");");
            }

            writer.WriteLine("</script>");
        }
        
        #endregion

        #region Protected Functions

        protected abstract string GetMethod();

        #endregion
    }
}
